class CulturalHeritage::IncidencesController < ApplicationController
#  before_filter :authenticate_user!
  #before_filter :authenticate_user!
  # GET /cultural_heritage/incidences
  # GET /cultural_heritage/incidences.xml
  def index
    @title_view = 'Incidencias'
    @cultural_heritage_incidences = CulturalHeritage::Incidence.all

    #Esto debiera extraesrse de la BD
    @lista_select = CulturalHeritage::CulturalHeritage.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_incidences }
    end
  end

  # GET /cultural_heritage/incidences/1
  # GET /cultural_heritage/incidences/1.xml
    def show
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
    if !(@cultural_heritage_incidence.person1.blank?)
      @persona1 =  Security::Person.find(@cultural_heritage_incidence.person1)
    end
    if !(@cultural_heritage_incidence.person2.blank?)
      @persona2 =  Security::Person.find(@cultural_heritage_incidence.person2)
    end
    @json = @cultural_heritage_incidence.to_gmaps4rails
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_incidence }
    end
  end

  # GET /cultural_heritage/incidences/new
  # GET /cultural_heritage/incidences/new.xml
  def new
    @cultural_heritage_incidence = CulturalHeritage::Incidence.new
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @cultural_heritage_incidence }
    end
  end

  # GET /cultural_heritage/incidences/1/edit
  def edit
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all    
  end

  # POST /cultural_heritage/incidences
  # POST /cultural_heritage/incidences.xml
  def create
    @cultural_heritage_incidence = CulturalHeritage::Incidence.new(params[:cultural_heritage_incidence])
    if !(@cultural_heritage_incidence.longitude.blank? and @cultural_heritage_incidence.latitude.blank?)
      @cultural_heritage_incidence.gmaps = true
    else
      @cultural_heritage_incidence.gmaps = false
      @cultural_heritage_incidence.longitude = nil
      @cultural_heritage_incidence.latitude = nil
    end
    respond_to do |format|
      if @cultural_heritage_incidence.save
        format.html { redirect_to(@cultural_heritage_incidence, :notice => 'La incidencia fue satisfactoriamente creada.') }
        format.xml  { render :xml => @cultural_heritage_incidence, :status => :created, :location => @cultural_heritage_incidence }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_incidence.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /cultural_heritage/incidences/1
  # PUT /cultural_heritage/incidences/1.xml
  def update
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
     if @cultural_heritage_incidence.state == 1
              @cultural_heritage_incidence.state = 3
              @cultural_heritage_incidence.save
     elsif @cultural_heritage_incidence.state == 3
              @cultural_heritage_incidence.state = 4
              @cultural_heritage_incidence.save
     else @cultural_heritage_incidence.state == 4
              @cultural_heritage_incidence.state = 5
              @cultural_heritage_incidence.save
     end

      respond_to do |format|
        if @cultural_heritage_incidence.update_attributes(params[:cultural_heritage_incidence])
          format.html { redirect_to(@cultural_heritage_incidence, :notice => 'La incidencia fue satisfactoriamente actualizada.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @cultural_heritage_incidence.errors, :status => :unprocessable_entity }
        end
      end
  end

  def update_table
    @search_id = params[:search_id]
    @search_combo = params[:search_incidence_cultural_heritage_id]
    if ! @search_id.blank?
      if ! @search_combo.blank?
        @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.id = ? and cultural_heritage_incidences.culturalHeritage = ? ",@search_id,@search_combo])
      else
        @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.id = ? ",@search_id])
      end
    else
      if !@search_combo.blank?
        @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ? ",@search_combo])
      else
        @cultural_heritage_incidences = CulturalHeritage::Incidence.all
      end
    end
    render :partial => "table"
  end

  def gallery
    @title_view = 'Incidencias'
    @cultural_heritage_incidence = CulturalHeritage::IncidencesAvatar.find(params[:id])
    @cultural_heritage_incidence_avatars = @cultural_heritage_incidence.list_avatars
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_incidence_avatars }
    end
  end

  def new_photo
    @title_view = 'Incidencia'
    @cultural_heritage_incidence_avatar = CulturalHeritage::IncidencesAvatar.new()
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
    @cultural_heritage_incidence_avatar.incidence = params[:id]
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @cultural_heritage_incidence_avatar }
    end
  end

  def create_photo
    @title_view = 'Patrimonio Cultural Inmueble'
    @cultural_heritage_incidence_avatar = CulturalHeritage::IncidencesAvatar.new(params[:cultural_heritage_cultural_heritage_avatar])
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
    @cultural_heritage_incidence_avatar.incidence = params[:id]
    respond_to do |format|
      if @cultural_heritage_incidence_avatar.save
        format.html { redirect_to(:controller=>"/cultural_heritage/incidences",:action => 'gallery',:id => @cultural_heritage_incidence_avatar.incidence, :notice => 'Foto fue satisfactoriamente guardada.') }
        format.xml  { render :xml => @cultural_heritage_incidence_avatar, :status => :created, :location => @cultural_heritage_incidence_avatar }
      else
        format.html { render :action => "new_photo",:controller=>"/cultural_heritage/incidences"}
        format.xml  { render :xml => @cultural_heritage_incidence_avatar.errors, :status => :unprocessable_entity }
      end
    end
  end

  def delete_photo
    @cultural_heritage_incidence_avatar = CulturalHeritage::IncidencesAvatar.find(params[:id])
    @cultural_heritage_incidence_avatar.deleted = 1
    @cultural_heritage_incidence_avatar.save
    respond_to do |format|
      format.html { redirect_to(:controller=>"/cultural_heritage/incidences",:action => 'gallery',:id =>@cultural_heritage_incidence_avatar.incidence) }
      format.xml  { head :ok }
    end
  end

  # DELETE /cultural_heritage/incidences/1
  # DELETE /cultural_heritage/incidences/1.xml
  def destroy
    @cultural_heritage_incidence = CulturalHeritage::Incidence.find(params[:id])
    @cultural_heritage_incidence.deleted = 1
    @cultural_heritage_incidence.save
    #@cultural_heritage_incidence.destroy

    respond_to do |format|
      format.html { redirect_to(cultural_heritage_incidences_url) }
      format.xml  { head :ok }
    end
  end

  def attend_incidence
    @incidence = CulturalHeritage::Incidence.find(params[:id])
    @incidence.state=1
    @incidence.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_incidences_url) }
      format.xml  { head :ok }
    end   
    #IncidenceMailer.attend_email(@incidence).deliver
  end

  def attend_incidence_inc
    @incidence = CulturalHeritage::Incidence.find(params[:id])
    @lista_personas = Security::Person.all
    @cultural_heritage  = CulturalHeritage::CulturalHeritage.find(@incidence.culturalHeritage)
    @estado = @incidence.state
    @cultural_heritage_cultural_heritage_avatars = @cultural_heritage.list_avatars
    #@incidence.state=3
    #@incidence.save
    respond_to do |format|
        format.html { render :layout => "blank_fullwidth"}
  #      format.xml  { render :xml => @lista_personas }
  #      format.xml  { render :json => @lista_personas }
    end
    #IncidenceMailer.attend_email(@incidence).deliver
  end

    def attend_incidence_low
    @incidence = CulturalHeritage::Incidence.find(params[:id])
    @incidence.state=4
    @incidence.save
    respond_to do |format|
      format.html format.html { render :action => "edit" }
      format.xml  { head :ok }
    end
    IncidenceMailer.attend_email(@incidence).deliver
  end
    

  def deny_incidence
    @incidence = CulturalHeritage::Incidence.find(params[:id])
    @incidence.state=2
    @incidence.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_incidences_url) }
      format.xml  { head :ok }
    end
    IncidenceMailer.attend_email(@incidence).deliver
  end

end
